<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <meta name="author" content="Rogerspy">
  <link rel="canonical" href="https://pytorch-zh.gitee.io/torchnn/moduledict/">
  <link rel="shortcut icon" href="/pytorch-zh/img/favicon.ico">
  <title>Moduledict - Pytorch 中文文档（1.4.0）</title>
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700|Roboto+Slab:400,700|Inconsolata:400,700" />

  <link rel="stylesheet" href="../../css/theme.css" />
  <link rel="stylesheet" href="../../css/theme_extra.css" />
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github.min.css" />
  
  <script>
    // Current page data
    var mkdocs_page_name = "Moduledict";
    var mkdocs_page_input_path = "torchnn\\moduledict.md";
    var mkdocs_page_url = "/torchnn/moduledict/";
  </script>
  
  <script src="../../js/jquery-2.1.1.min.js" defer></script>
  <script src="../../js/modernizr-2.8.3.min.js" defer></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
  <script>hljs.initHighlightingOnLoad();</script> 
  
</head>

<body class="wy-body-for-nav" role="document">

  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav">
    <div class="wy-side-scroll">
      <div class="wy-side-nav-search">
	    <img src='/pytorch-zh/img/logo.svg'/>
        <a href="../.." class="icon icon-home"> Pytorch 中文文档（1.4.0）</a>
        <div role="search">
  <form id ="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" title="Type search term here" />
  </form>
</div>
      </div>

      <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="../..">主页</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="../../get-started/">60分钟快速入门</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="../../faq/">PyTorch FAQ</a>
                    </li>
                </ul>
                <p class="caption"><span class="caption-text">两种基本结构</span></p>
                <ul class="current">
                    <li class="toctree-l1"><a class="reference internal" href="../parameters/">参数 Parameters</a>
                    </li>
                    <li class="toctree-l1 current"><a class="reference internal current" href="#">容器 Containers</a>
    <ul class="current">
                <li class="toctree-l2"><a class="reference internal" href="../module/">Module</a>
                </li>
                <li class="toctree-l2"><a class="reference internal" href="../sequential/">Sequential</a>
                </li>
                <li class="toctree-l2"><a class="reference internal" href="../modulelist/">Modulelist</a>
                </li>
                <li class="toctree-l2 current"><a class="reference internal current" href="./">Moduledict</a>
    <ul class="current">
    <li class="toctree-l3"><a class="reference internal" href="#clear">clear</a>
    </li>
    <li class="toctree-l3"><a class="reference internal" href="#items">items</a>
    </li>
    <li class="toctree-l3"><a class="reference internal" href="#keys">keys</a>
    </li>
    <li class="toctree-l3"><a class="reference internal" href="#pop">pop</a>
    </li>
    <li class="toctree-l3"><a class="reference internal" href="#update">update</a>
    </li>
    <li class="toctree-l3"><a class="reference internal" href="#value">Value</a>
    </li>
    </ul>
                </li>
                <li class="toctree-l2"><a class="reference internal" href="../parameterlist/">Parameterlist</a>
                </li>
                <li class="toctree-l2"><a class="reference internal" href="../parameterdict/">Parameterdict</a>
                </li>
    </ul>
                    </li>
                </ul>
                <p class="caption"><span class="caption-text">网络层</span></p>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="#">卷积层</a>
    <ul>
                <li class="toctree-l2"><a class="reference internal" href="../conv1d/">Conv1d</a>
                </li>
                <li class="toctree-l2"><a class="reference internal" href="../conv2d/">Conv2d</a>
                </li>
                <li class="toctree-l2"><a class="reference internal" href="../conv3d/">Conv3d</a>
                </li>
                <li class="toctree-l2"><a class="reference internal" href="../convtranspose1d/">Convtranspose1d</a>
                </li>
                <li class="toctree-l2"><a class="reference internal" href="../convtranspose2d/">Convtranspose2d</a>
                </li>
                <li class="toctree-l2"><a class="reference internal" href="../convtranspose3d/">Convtranspose3d</a>
                </li>
    </ul>
                    </li>
                </ul>
      </div>
    </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
        <a href="../..">Pytorch 中文文档（1.4.0）</a>
      </nav>

      
      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="breadcrumbs navigation">
  <ul class="wy-breadcrumbs">
    <li><a href="../..">Docs</a> &raquo;</li>
    
      
        
          <li>容器 Containers &raquo;</li>
        
      
        
          <li>两种基本结构 &raquo;</li>
        
      
    
    <li>Moduledict</li>
    <li class="wy-breadcrumbs-aside">
      
    </li>
  </ul>
  
  <hr/>
</div>
          <div role="main">
            <div class="section">
              
                <h2 id="moduledict">ModuleDict<a class="headerlink" href="#moduledict" title="Permanent link">&para;</a></h2>
<div class='important'>
    <pre>
    <font color='red'>CLASS</font> torch.nn.ModuleDict(
        <i>modules: Optional[
                Mapping[str, torch.nn.modules.module.Module]
            ] = None</i>
        )</pre>
</div>

<p>将子模块包装成字典。</p>
<p><code>ModuleDict</code> 可以使用常规的 Python 字典进行索引， 但是它包含的模块是正确注册的，并且对所有 <code>Module</code> 方法可见。</p>
<p><code>ModuleDict</code> 是一个有序字典。遵守</p>
<ul>
<li>插入顺序，以及</li>
<li>在 <code>update()</code> 中，<code>OderedDict</code> 或另一个 <code>ModuleDict</code>  的 合并顺序</li>
</ul>
<p>注意，<code>update()</code> 和其他无序映射类型（例如 Python 的普通 dict）不会保留合并映射顺序。</p>
<blockquote>
<p><strong><em>参数</em></strong></p>
<ul>
<li><em>modules (iterable, optional)</em> ：一个键值对类型的可迭代映射。</li>
</ul>
</blockquote>
<p>例子：</p>
<div class="codehilite"><pre><span></span><code><span class="k">class</span> <span class="nc">MyModule</span><span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">MyModule</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">choices</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">ModuleDict</span><span class="p">({</span>
            <span class="s1">&#39;conv&#39;</span><span class="p">:</span> <span class="n">nn</span><span class="o">.</span><span class="n">Conv2d</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span>
            <span class="s1">&#39;pool&#39;</span><span class="p">:</span> <span class="n">nn</span><span class="o">.</span><span class="n">MaxPool2d</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
        <span class="p">})</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">activations</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">ModuleDict</span><span class="p">([</span>
            <span class="p">[</span><span class="s1">&#39;lrelu&#39;</span><span class="p">,</span> <span class="n">nn</span><span class="o">.</span><span class="n">LeakReLU</span><span class="p">()],</span>
            <span class="p">[</span><span class="s1">&#39;prelu&#39;</span><span class="p">,</span> <span class="n">nn</span><span class="o">.</span><span class="n">PReLU</span><span class="p">()]</span>
        <span class="p">])</span>

    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">choice</span><span class="p">,</span> <span class="n">act</span><span class="p">):</span>
        <span class="n">x</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">choices</span><span class="p">[</span><span class="n">choice</span><span class="p">](</span><span class="n">x</span><span class="p">)</span>
        <span class="n">x</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">activations</span><span class="p">[</span><span class="n">act</span><span class="p">](</span><span class="n">x</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">x</span>
</code></pre></div>

<h3 id="clear">clear<a class="headerlink" href="#clear" title="Permanent link">&para;</a></h3>
<blockquote>
<div class="codehilite"><pre><span></span><code><span class="n">clear</span><span class="p">()</span> <span class="err">→</span> <span class="kc">None</span>
</code></pre></div>

<p>移除 ModuleDict 中的所有元素。</p>
</blockquote>
<h3 id="items">items<a class="headerlink" href="#items" title="Permanent link">&para;</a></h3>
<blockquote>
<div class="codehilite"><pre><span></span><code><span class="n">items</span><span class="p">()</span> <span class="err">→</span>  <span class="n">Iterable</span><span class="p">[</span><span class="n">Tuple</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">torch</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">modules</span><span class="o">.</span><span class="n">module</span><span class="o">.</span><span class="n">Module</span><span class="p">]]</span>
</code></pre></div>

<p>返回可迭代的 ModuleDict 键值对。</p>
</blockquote>
<h3 id="keys">keys<a class="headerlink" href="#keys" title="Permanent link">&para;</a></h3>
<blockquote>
<div class="codehilite"><pre><span></span><code><span class="n">keys</span><span class="p">()</span> <span class="err">→</span> <span class="n">Iterable</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span>
</code></pre></div>

<p>返回 ModuleDict 中所有的键。</p>
</blockquote>
<h3 id="pop">pop<a class="headerlink" href="#pop" title="Permanent link">&para;</a></h3>
<blockquote>
<div class="codehilite"><pre><span></span><code><span class="n">pop</span><span class="p">(</span><span class="n">key</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="err">→</span> <span class="n">torch</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">modules</span><span class="o">.</span><span class="n">module</span><span class="o">.</span><span class="n">Module</span>
</code></pre></div>

<p>从 ModuleDict 中移除对应的键，并且返回对应的模块。</p>
<p><strong><em>参数</em></strong></p>
<ul>
<li><em>key (string)</em>：需要移除的键</li>
</ul>
</blockquote>
<h3 id="update">update<a class="headerlink" href="#update" title="Permanent link">&para;</a></h3>
<blockquote>
<div class="codehilite"><pre><span></span><code><span class="n">update</span><span class="p">(</span><span class="n">modules</span><span class="p">:</span> <span class="n">Mapping</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">torch</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">modules</span><span class="o">.</span><span class="n">module</span><span class="o">.</span><span class="n">Module</span><span class="p">])</span> <span class="err">→</span> <span class="kc">None</span>
</code></pre></div>

<p>使用一个映射或者可迭代的键值对，来更新或者覆盖 ModuleDict 已有的键值对。</p>
<p><div class='note'> 如果 <code> modules</code> 是一个 <code>ModuleDict </code>或者是一个新的键值对，那么他们的顺序会被保留。</div></p>
<p><strong><em>参数</em></strong></p>
<ul>
<li><em>modules (iterable)</em>： 从字符串到模块的映射，或者可迭代的（string, <code>Module</code>）类型的键值对</li>
</ul>
</blockquote>
<h3 id="value">Value<a class="headerlink" href="#value" title="Permanent link">&para;</a></h3>
<blockquote>
<div class="codehilite"><pre><span></span><code><span class="n">values</span><span class="p">()</span> <span class="err">→</span> <span class="n">Iterable</span><span class="p">[</span><span class="n">torch</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">modules</span><span class="o">.</span><span class="n">module</span><span class="o">.</span><span class="n">Module</span><span class="p">]</span>
</code></pre></div>

<p>返回 ModuleDict 的可迭代的值。</p>
</blockquote>
              
            </div>
          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="../parameterlist/" class="btn btn-neutral float-right" title="Parameterlist">Next <span class="icon icon-circle-arrow-right"></span></a>
      
      
        <a href="../modulelist/" class="btn btn-neutral" title="Modulelist"><span class="icon icon-circle-arrow-left"></span> Previous</a>
      
    </div>
  

  <hr/>

  <div role="contentinfo">
    <!-- Copyright etc -->
    
      <p>©2020 Rogerspy. All rights reserved.</p>
    
  </div>

  Built with <a href="https://www.mkdocs.org/">MkDocs</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
      
        </div>
      </div>

    </section>

  </div>

  <div class="rst-versions" role="note" aria-label="versions">
    <span class="rst-current-version" data-toggle="rst-current-version">
      
      
        <span><a href="../modulelist/" style="color: #fcfcfc;">&laquo; Previous</a></span>
      
      
        <span style="margin-left: 15px"><a href="../parameterlist/" style="color: #fcfcfc">Next &raquo;</a></span>
      
    </span>
</div>
    <script>var base_url = '../..';</script>
    <script src="../../js/theme.js" defer></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS-MML_HTMLorMML" defer></script>
      <script src="../../search/main.js" defer></script>
    <script defer>
        window.onload = function () {
            SphinxRtdTheme.Navigation.enable(true);
        };
    </script>

</body>
</html>
